package com.supor.aiot.home.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.supor.aiot.home.R;

public class ConfigFailedDialog extends Dialog {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusTextColorDark(this);
    }

    public static void setStatusTextColorDark(Dialog dialog) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            dialog.getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            dialog.getWindow().setStatusBarColor(Color.BLACK);
        }
    }

    public ConfigFailedDialog(Context context, int theme) {
        super(context, theme);
    }

    public ConfigFailedDialog(Context context) {
        super(context);
    }

    public static class Builder {
        private Context context;

        private View contentView;

        private View layout = null;

        private String title;

        private String message;

        private String positiveButtonText;

        private int negativeColor = 0;

        private String negativeButtonText;

        private String hint = "";

        private OnClickListener positiveButtonClickListener;

        private OnClickListener negativeButtonClickListener;

        private Boolean isHintPostivieBtn = false;

        /**
         * 默认底部弹框样式
         */
        private boolean bottomStyle = true;
        /**
         * 默认均分button宽度
         */
        private boolean averageButton = true;

        /**
         * 构造函数<br>
         * <br>
         *
         * @param context     active
         * @see [相关类，可选、也可多条，对于重要的类或接口建议注释]
         * @since LinkHome, 2015-8-19
         */
        public Builder(Context context) {
            this.context = context;
        }

        public void setBottomStyle(boolean bottomStyle) {
            this.bottomStyle = bottomStyle;
        }

        public void setAverageButton(boolean averageButton) {
            this.averageButton = averageButton;
        }

        public String getHint() {
            return hint;
        }

        public void setHint(String hint) {
            this.hint = hint;
        }

        public Builder setIsHintPostivieBtn(Boolean flag) {
            this.isHintPostivieBtn = flag;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @param //title
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        public Builder setNegativeColor(int color) {
            this.negativeColor = color;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText, OnClickListener listener) {
            this.positiveButtonText = (String) context.getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText, OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = (String) context.getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public ConfigFailedDialog create() {
            LayoutInflater inflater =
                    (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final ConfigFailedDialog dialog = new ConfigFailedDialog(context, R.style.appBasicDialog);
            WindowManager m = ((Activity) context).getWindowManager();
            Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
            Point point = new Point();
            d.getSize(point);
            WindowManager.LayoutParams params =
                    dialog.getWindow().getAttributes();  //获取对话框当前的参数值、

            layout = inflater.inflate(R.layout.dialog_config_failed, null);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT));

            params.width = (int) (point.x);    //宽度设置全屏宽度
            dialog.getWindow().setAttributes(params);     //设置生效
            dialog.getWindow().setGravity(Gravity.BOTTOM);
            dialog.getWindow().setWindowAnimations(R.style.bottom_window_anim);

            Button positiveBtn = layout.findViewById(R.id.positive_btn);
            Button negativeBtn = layout.findViewById(R.id.negative_btn);

            // set the confirm button
            if (positiveButtonText != null) {
                positiveBtn.setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    positiveBtn.setOnClickListener(v -> {
                        positiveButtonClickListener.onClick(dialog,
                                DialogInterface.BUTTON_POSITIVE);
                    });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                positiveBtn.setVisibility(View.GONE);
                layout.findViewById(R.id.btn_vertical_line).setVisibility(View.GONE);
            }
            // set the cancel button
            if (negativeButtonText != null) {
                negativeBtn.setText(negativeButtonText);
                if (negativeColor != 0) {
                    negativeBtn.setTextColor(negativeColor);
                }
                if (negativeButtonClickListener != null) {
                    negativeBtn.setOnClickListener(v -> negativeButtonClickListener
                                    .onClick(dialog, DialogInterface.BUTTON_NEGATIVE));
                }
            } else {
                // if no confirm button just set the visibility to GONE
                negativeBtn.setVisibility(View.GONE);
                layout.findViewById(R.id.btn_vertical_line).setVisibility(View.GONE);
            }

            // set the content message
            if (message != null) {
                TextView info1 = layout.findViewById(R.id.dialog_info1);
                String tip = message;
                SpannableString ss = new SpannableString(tip);

                ss.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorPrimary)),
                        tip.indexOf("信息") + 2, tip.indexOf("是否正确"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ss.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorPrimary)),
                        tip.indexOf("重新配网") - 1, tip.indexOf("重新配网") + 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                info1.setText(ss);
            }

            dialog.setContentView(layout);
            return dialog;
        }

    }

}
